import json
import socket
import urllib.request
import urllib.error

VERSION = "1.1.0"

def check_internet_connection(timeout=3):
    """检查是否有网络连接"""
    try:
        socket.setdefaulttimeout(timeout)
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("8.8.8.8", 53))
        return True
    except (socket.timeout, socket.error):
        return False

def check_update(parent=None):
    """
    检查更新
    :return: 如果有更新返回配置信息，否则返回None
    """
    try:
        # 从GitHub获取最新版本信息
        url = "https://api.github.com/repos/WEIFENG2333/AsrTools/releases/latest"
        headers = {'User-Agent': 'Mozilla/5.0'}
        request = urllib.request.Request(url, headers=headers)
        response = urllib.request.urlopen(request, timeout=5)
        data = json.loads(response.read().decode('utf-8'))
        
        latest_version = data['tag_name'].replace('v', '')
        current_version = VERSION
        
        if latest_version > current_version:
            return {
                'version': latest_version,
                'update_download_url': data['html_url'],
                'description': data['body'],
                'fource': False  # 是否强制更新
            }
        return None
    except (urllib.error.URLError, json.JSONDecodeError, KeyError, Exception):
        return None 